<?php
abstract class MenuActiveRecord extends CActiveRecord{

    protected function purify($text)
    {
        $p = new CHtmlPurifier;
        $p->options = array(
            'HTML.SafeObject'=>true,
            'Output.FlashCompat'=>true,
        );
        return $p->purify($text);
    }

    protected function beforeSave()
    {
        if(parent::beforeSave())
        {
            $this->item_name = $this->purify($this->item_name);
            $this->title= $this->purify($this->title);
            $this->link_title = $this->purify($this->link_title);
            $this->description = $this->purify($this->description);
            return true;
        }
        else
            return false;
    }

    //Удаляем вьюшки
    protected  function beforeDelete()
    {
        if(Yii::app()->controller->id === 'backend')
        {
            $view_file = trim(strrchr($this->link_path,'/'),'/');
            $path = Yii::app()->getModule('menu')->basePath.'/views/frontend/'.$view_file.'.php';
            $path_ = Yii::app()->getModule('menu')->basePath.'/views/frontend/_'.$view_file.'.php';
            if(file_exists($path))
            {
                @unlink($path);
            }
            if(file_exists($path_))
            {
                @unlink($path_);
            }
        }

        return parent::beforeDelete();
    }


    public function behaviors()
    {
        return array(
            'CTimestampBehavior' => array(
                'class'=>'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_time',
                'updateAttribute' => 'update_time',
                'setUpdateOnCreate' => true,
            )
        );
    }
}